/**
 * outsider.com.cn Inc.
 * Copyright (c) 2011-2022 All Rights Reserved.
 */
package com.yuanmeng.common.core.handler;

import java.math.BigDecimal;
import java.util.Date;

import com.yuanmeng.common.core.enums.ErrorCodeEnum;
import com.yuanmeng.common.core.exception.ServiceException;
import com.yuanmeng.common.core.utils.DateUtils;

/**
 * @author outsider
 * @version Id: OrderValidateHandler.java, v 0.1 2022/11/8 14:31 outsider Exp $$
 */
public class OrderValidateHandler {
    /**
     * 订单过期支付校验
     * @param timeExpire
     */
    public static void orderTimeExpireValidate(Date timeExpire) {
        if (null != timeExpire) {
            if (timeExpire.before(DateUtils.getNowDate())) {
                throw new ServiceException(ErrorCodeEnum.CURRENT_ORDER_ALREADY_EXPIRED);
            }
        }
    }

    /**
     * 支付金额校验
     * @param requestPayAmount
     * @param orderPayAmount
     */
    public static void payAmountValidate(BigDecimal requestPayAmount, BigDecimal orderPayAmount) {
        if (requestPayAmount.compareTo(orderPayAmount) != 0) {
            throw new ServiceException(ErrorCodeEnum.PAY_AMOUNT_ERROR);
        }
    }

    /**
     * 退款金额校验
     * @param requestPayAmount
     * @param orderPayAmount
     */
    public static void refundAmountValidate(BigDecimal requestPayAmount, BigDecimal orderPayAmount) {
        if (requestPayAmount.compareTo(orderPayAmount) != 0) {
            throw new ServiceException(ErrorCodeEnum.REFUND_AMOUNT_ERROR);
        }
    }

    /**
     * 余额支付金额校验
     * @param payAmount         待支付金额
     * @param balanceAmount    账户余额
     */
    public static void balancePayAmountValidate(BigDecimal payAmount, BigDecimal balanceAmount) {
        if (payAmount.compareTo(balanceAmount) == 1) {
            throw new ServiceException(ErrorCodeEnum.BALANCE_INSUFFICIENT_ERROR);
        }
    }

    /**
     * 支付积分数量校验
     * @param requestPointNum
     * @param orderPointNum
     */
    public static void payPointNumValidate(Long requestPointNum, Long orderPointNum) {
        if (!requestPointNum.equals(orderPointNum)) {
            throw new ServiceException(ErrorCodeEnum.PAY_POINT_NUM_ERROR);
        }
    }

    /**
     * 退款积分数量校验
     * @param requestPointNum
     * @param orderPointNum
     */
    public static void refundPointNumValidate(Long requestPointNum, Long orderPointNum) {
        if (!requestPointNum.equals(orderPointNum)) {
            throw new ServiceException(ErrorCodeEnum.REFUND_POINT_ERROR);
        }
    }

    /**
     * 积分余额数量校验
     * @param requestPointNum
     * @param pointBalance
     */
    public static void pointBalanceValidate(Long requestPointNum, Long pointBalance) {
        if (pointBalance < requestPointNum) {
            throw new ServiceException(ErrorCodeEnum.BALANCE_POINT_INSUFFICIENT_ERROR);
        }
    }
}